<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><title>Magic with Merlin: Swing's new JFormattedTextField component</title><meta http-equiv="PICS-Label" content="(PICS-1.1 &quot;http://www.icra.org/ratingsv02.html&quot; l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) &quot;http://www.rsac.org/ratingsv01.html&quot; l gen true r (n 0 s 0 v 0 l 0) &quot;http://www.classify.org/safesurf/&quot; l gen true r (SS~~000 1))">
<link rel="schema.DC" href="http://purl.org/DC/elements/1.0/">
<link rel="SHORTCUT ICON" href="http://www.ibm.com/favicon.ico">
<meta name="Owner" content="dW Information/Raleigh/IBM">
<meta name="DC.Language" scheme="rfc1766" content="en-US">
<meta name="IBM.Country" content="ZZ">
<meta name="Security" content="Public"><!-- <meta name="IBM.SpecialPurpose" content="SP001" />
<meta name="IBM.PageAttributes" content="sid=1003"/> -->

<meta name="Source" content="Based on v14 Template Generator, Template 14">
<meta name="Abstract" content="Accepting formatted input doesn't have to be difficult with input verifiers and focus listeners, as this article shows."><meta name="Description" content="Accepting formatted input doesn't have to be difficult with input verifiers and focus listeners, as this article shows.f
      "><meta name="Keywords" content="John Zukowski, Merlin, J2SE, Java 2 Standard Edition, Java 1.4, JDK 1.4, Swing, Spinner, tttjca"><meta name="DC.Date" scheme="iso8601" content="2002-06-01"><meta name="DC.Type" scheme="IBM_ContentClassTaxonomy" content="CT316"><meta name="DC.Subject" scheme="IBM_SubjectTaxonomy" content=""><meta name="DC.Rights" content="Copyright (c) 2002 by IBM Corporation"> <meta name="Robots" content="index,follow"><meta name="IBM.Effective" scheme="W3CDTF" content="2002-06-01"><meta name="Last update" content="22032006mwfergus@us.ibm.com"><!-- STYLESHEETS/SCRIPTS --><!-- for tables -->

<link rel="stylesheet" type="text/css" media="screen,print" href="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/table.css"><!-- end for tables --> 

<script language="JavaScript" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/dwcss14.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/main.css">
<link rel="stylesheet" type="text/css" media="all" href="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/screen.css">
<link rel="stylesheet" type="text/css" media="print" href="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/print.css">
<script language="JavaScript" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/detection.js" type="text/javascript"></script>
<script language="JavaScript" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/dropdown.js" type="text/javascript"></script>
<script language="JavaScript" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/grabtitle.js" type="text/javascript"></script>
<script language="JavaScript" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/emailfriend2.js" type="text/javascript"></script><script language="JavaScript" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/urltactic.js" type="text/javascript"></script><script language="JavaScript" type="text/javascript">
 <!--
 setDefaultQuery('javaart');
 //-->
</script><!--START RESERVED FOR FUTURE USE INCLUDE FILES-->
<script language="javascript" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/ajax1.js" type="text/javascript"></script><script language="javascript" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/searchcount.js" type="text/javascript"></script><!--END RESERVED FOR FUTURE USE INCLUDE FILES--><script language="JavaScript" type="text/javascript">var emailAbstract = "Accepting formatted input doesn't have to be difficult with input verifiers and focus listeners, as this article shows."; </script></head><body><!--MASTHEAD_BEGIN--><table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody><tr valign="top">
<td class="bbg" width="110"><a style="" href="http://www.ibm.com/"><img alt="IBM®" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/ibm-logo.gif" border="0" height="52" width="110"></a></td>
<td class="bbg"><img src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" alt="" border="0" height="1" width="1"></td>
<td class="mbbg" align="right" width="650">
<table align="right" border="0" cellpadding="0" cellspacing="0">
<tbody><tr class="cty-tou">
<td rowspan="2" class="upper-masthead-corner" width="17"><a href="#main"><img src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" alt="Skip to main content" border="0" height="1" width="1"></a></td>
<td align="left">
<table align="left" border="0" cellpadding="0" cellspacing="0">
<tbody><tr>
<td><span class="spacer">&nbsp;&nbsp;&nbsp;&nbsp;</span><b class="country">Country/region</b><span class="spacer">&nbsp;[</span><a class="ur-link" href="http://www.ibm.com/developerworks/country/">select</a><span class="spacer">]</span></td>
<td class="upper-masthead-divider" width="29">&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td align="left"><a class="ur-link" href="http://www.ibm.com/legal/">Terms of use</a></td>
</tr>
</tbody></table>
</td>
<td width="40">&nbsp;</td>
</tr>
<tr>
<td class="cty-tou-border" colspan="2" height="1"><img src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" alt="" height="1" width="1"></td>
</tr>
<tr>
<td colspan="3"><img alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" height="8" width="1"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td colspan="2" align="center">
<input name="searchType" value="1" type="hidden"><input name="searchSite" value="dW" type="hidden">
<table border="0" cellpadding="0" cellspacing="0">
<tbody><tr>
<form method="get" action="//www.ibm.com/developerworks/search/searchResults.jsp" id="form1" name="form1"></form><td width="18"><label for="q"><img src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" alt="Search in:" height="1" width="1"></label></td>
<td align="right"><select id="sq" name="searchScope" class="input-local" size="1">
<option value="dW" selected="selected">All of dW</option>
<option value="dW">-----------------</option>
<option value="aixunix">&nbsp;&nbsp;AIX and UNIX</option>
<option value="eserver">&nbsp;&nbsp;IBM Systems</option>
<option value="db2">&nbsp;&nbsp;Information Mgmt</option>
<option value="lotus">&nbsp;&nbsp;Lotus</option>
<option value="rdd">&nbsp;&nbsp;Rational</option>
<option value="tivoli">&nbsp;&nbsp;Tivoli</option>  
<option value="WSDD">&nbsp;&nbsp;WebSphere</option>
<option value="dW">-----------------</option> 
<option value="archZ">&nbsp;&nbsp;Architecture</option>
<option value="acZ">&nbsp;&nbsp;Autonomic computing</option>
<option value="gridZ">&nbsp;&nbsp;Grid computing</option>
<option value="javaZ">&nbsp;&nbsp;Java technology</option> 
<option value="linuxZ">&nbsp;&nbsp;Linux</option> 
<option value="opensrcZ">&nbsp;&nbsp;Open source</option>
<option value="paZ">&nbsp;&nbsp;Power Architecture</option>
<option value="webservZ">&nbsp;&nbsp;SOA &amp; Web services</option>
<option value="webarchZ">&nbsp;&nbsp;Web development</option>  
<option value="xmlZ">&nbsp;&nbsp;XML</option>
<option value="dW">-----------------</option>
<option value="forums">&nbsp;&nbsp;dW forums</option> 
<option value="dW">-----------------</option>
<option value="aW">alphaWorks</option>
<option value="dW">-----------------</option>
<option value="all">All of IBM</option>
</select></td>
<td align="right" width="7"><label for="q"><img src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" alt="Search for:" height="1" width="1"></label>&nbsp;&nbsp;</td>
<td align="right"><input class="input" id="q" maxlength="100" name="query" size="15" value="" type="text">
</td>
<td width="7">&nbsp;
</td>
<td width="90"><input alt="Search" name="Search" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/search.gif" value="Search" type="image"></td>
<!-- <td width="20">&nbsp;</td> -->

</tr>
</tbody></table>
</td>
</tr>
</tbody></table>
</td>
</tr>
<tr>
<td class="blbg" colspan="3">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody><tr>
<td>
<table border="0" cellpadding="0" cellspacing="0">
<tbody><tr>
<td><span class="spacer">&nbsp;&nbsp;&nbsp;&nbsp;</span></td>
<td><a class="masthead-mainlink" href="http://www.ibm.com/">Home</a></td>
<td class="masthead-divider" width="27">&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td><a class="masthead-mainlink" href="http://www.ibm.com/products/">Products</a></td>
<td class="masthead-divider" width="27">&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td><a class="masthead-mainlink" href="http://www.ibm.com/servicessolutions/">Services &amp; industry solutions</a></td>
<td class="masthead-divider" width="27">&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td><a class="masthead-mainlink" href="http://www.ibm.com/support/">Support &amp; downloads</a></td>
<td class="masthead-divider" width="27">&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td><a class="masthead-mainlink" href="http://www.ibm.com/account/">My IBM</a></td>
<td><span class="spacer">&nbsp;&nbsp;&nbsp;&nbsp;</span></td>
</tr>
</tbody></table>
</td>
</tr>
</tbody></table>
</td>
</tr>
</tbody></table>

<script src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/pmh.js" language="JavaScript" type="text/javascript"></script><!--MASTHEAD_END--><!-- CMA ID for this content is: 10675 --> <table id="v14-body-table" border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr valign="top"><!--LEFTNAV_BEGIN--><td id="navigation" width="150"><table border="0" cellpadding="0" cellspacing="0" width="150"><tbody><tr><td class="left-nav-spacer"><a href="http://www.ibm.com/developerworks" class="left-nav-overview">&nbsp;</a></td></tr></tbody></table><table border="0" cellpadding="0" cellspacing="0" width="150"><tbody><tr><td colspan="2" class="left-nav-overview"><a href="http://www.ibm.com/developerworks" class="left-nav-overview">developerWorks</a></td></tr></tbody></table><table border="0" cellpadding="0" cellspacing="0" width="150"><tbody><tr><td colspan="2" class="left-nav"><a class="left-nav" href="http://www.ibm.com/developerworks/views/java/libraryview.jsp?search_by=merlin:">More in this series:</a><br><a class="left-nav-child" href="http://www.ibm.com/developerworks/views/java/libraryview.jsp?search_by=merlin:">Magic with Merlin</a></td></tr><tr><td class="dw-left-nav-separator" width="14"><img alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" height="6" width="14"></td><td class="dw-left-nav-separator" width="136"><img alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" height="6" width="136"></td></tr><tr><td colspan="2" class="left-nav-highlight"><a href="#" class="left-nav">In this article:</a></td></tr><tr class="left-nav-child-highlight"><td><img alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/cl-bullet.gif" height="8" width="2"></td><td><a href="#1" class="left-nav-child">Configuring acceptable input</a></td></tr><tr class="left-nav-child-highlight"><td><img alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/cl-bullet.gif" height="8" width="2"></td><td><a href="#1" class="left-nav-child">Putting it all together</a></td></tr><tr class="left-nav-child-highlight"><td><img alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/cl-bullet.gif" height="8" width="2"></td><td><a href="#resources" class="left-nav-child">Resources</a></td></tr><tr class="left-nav-child-highlight"><td><img alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/cl-bullet.gif" height="8" width="2"></td><td><a href="#author" class="left-nav-child">About the author</a></td></tr><tr class="left-nav-child-highlight"><td><img alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/cl-bullet.gif" height="8" width="2"></td><td><a href="#rate" class="left-nav-child">Rate this page</a></td></tr><tr class="left-nav-last"><td width="14"><img class="display-img" alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" height="1" width="14"></td><td width="136"><img class="display-img" alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/left-nav-corner.gif" height="19" width="136"></td></tr></tbody></table><br><table border="0" cellpadding="0" cellspacing="0" width="150"><tbody><tr><td class="related" colspan="2"><b class="related">Related links</b></td></tr><tr class="rlinks"><td><img alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/rl-bullet.gif" height="8" width="2"></td><td><a class="rlinks" href="http://www.ibm.com/developerworks/views/java/library.jsp">Java technology technical library</a></td></tr><!--START RESERVED FOR FUTURE USE INCLUDE FILES--><!-- No content currently --><!--END RESERVED FOR FUTURE USE INCLUDE FILES--><tr><td width="14"><img class="display-img" alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" height="1" width="14"></td><td width="136"><img class="display-img" alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" height="19" width="136"></td></tr></tbody></table><!--START RESERVED FOR FUTURE USE INCLUDE FILES--><!-- Next Steps Area:  Start -->
<!-- Commented out the include call in the dwmaster version of this file to prevent ajax calls
     being made during article previews and testing.  Live site has uncommented copy of this file (jpp) -->
<!-- Call Next Steps Servlet -->
<script language="JavaScript" type="text/javascript">
<!--
/*
 * ajaxInclude makes a call to the url and render the results in the div tag specified in divId
 */
function ajaxInclude(url, divId) { 
 var req = newXMLHttpRequest(); 
 if (req) { 
   req.onreadystatechange = getReadyStateHandler(req, 
	function (result) {  
           var contents = document.getElementById(divId);  
           if (result != null && result.length > 0 && contents != null) {
	     contents.innerHTML = result;  
           }
        }); 
   req.open("GET", url, true);
   req.send("");
 }
}
//-->
</script>

<!-- Display Next Steps Result -->
<div id="nextsteps"></div>

<!-- Initiate Next Steps Call -->
<script language="JavaScript" type="text/javascript">
<!-- 
 ajaxInclude("/developerworks/niagara/jsp/getNiagaraContent.jsp?url="+window.location.href,"nextsteps");
//-->
</script>
<!-- Next Steps Area:  End --><!--END RESERVED FOR FUTURE USE INCLUDE FILES--></td><!--LEFTNAV_END--><td width="100%"><table id="content-table" border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr valign="top"><td width="100%"><table border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td><a name="main"><img alt="skip to main content" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" border="0" height="1" width="592"></a></td></tr></tbody></table><table border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr valign="top"><td height="18" width="10"><img alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" height="18" width="10"></td><td width="100%"><img alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" height="6" width="1"><br><a href="http://www.ibm.com/developerworks/" class="bctl">developerWorks</a><span class="bct">&nbsp;&nbsp;&gt;&nbsp;&nbsp;</span><a class="bctl" href="http://www.ibm.com/developerworks/java/">Java technology</a><span class="bct">&nbsp;&nbsp;&gt;</span><img alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" height="1" width="1"><br><h1><span style="color: rgb(153, 153, 153);">Magic with Merlin: </span>Swing's new JFormattedTextField component</h1><p id="subtitle"><em>Learn to create input fields that accept formatted text with minimal work</em></p><img alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" class="display-img" height="6" width="1"></td><td class="no-print" width="192"><a href="http://www.ibm.com/developerworks/"><img alt="developerWorks" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/dw.gif" border="0" height="18" width="192"></a></td></tr></tbody></table></td></tr></tbody></table><table border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr valign="top"><td width="10"><img alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" height="1" width="10"></td><td width="100%"><table class="no-print" align="right" border="0" cellpadding="0" cellspacing="0" width="160"><tbody><tr><td width="10"><img alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" height="1" width="10"></td><td><table border="0" cellpadding="0" cellspacing="0" width="150"><tbody><tr><td class="v14-header-1-small">Document options</td></tr></tbody></table><table class="v14-gray-table-border" border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="no-padding" width="150"><noscript></noscript><noscript></noscript><table border="0" cellpadding="0" cellspacing="0" width="143"><script language="JavaScript" type="text/javascript">
<!--
document.write('<tr valign="top"><td width="8"><img src="//www.ibm.com/i/c.gif" width="8" height="1" alt=""/></td><td width="16"><img alt="Set printer orientation to landscape mode" height="16" src="//www.ibm.com/i/v14/icons/printer.gif" width="16" vspace="3" /></td><td width="122"><p><b><a class="smallplainlink" href="javascript:print()">Print this page</a></b></p></td></tr>');
//-->
</script>
<script language="JavaScript" type="text/javascript">
<!-- 5.6 10/24 llk: added cdata around the subdirectory path of email gif-->
<!--
document.write('<tr valign="top"><td width="8"><img src="//www.ibm.com/i/c.gif" width="8" height="1" alt=""/></td><td width="16"><img src="//www.ibm.com/i/v14/icons/em.gif" height="16" width="16" vspace="3" alt="Email this page" /></td><td width="122"><p><a class="smallplainlink" href="javascript:void newWindow()"><b>E-mail this page</b></a></p></td></tr>');
//-->
</script><tbody><tr valign="top"><td width="8"><img alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" height="1" width="8"></td><td width="16"><img alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" height="16" width="16"></td><td class="small" width="122"><p><span class="ast">Document options requiring JavaScript are not displayed</span></p></td></tr><tr valign="top"><td width="8"><img alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" height="1" width="8"></td><td width="16"><img alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/fw_bold.gif" border="0" height="16" vspace="3" width="16"></td><td width="122"><p><a class="smallplainlink" href="http://www-106.ibm.com/developerworks/forums/dw_forum.jsp?forum=182&amp;cat=10"><b>Discuss</b></a></p></td></tr></tbody></table></td></tr></tbody></table><!--START RESERVED FOR FUTURE USE INCLUDE FILES--><!-- 11/28/07 refreshed by gem, per MOC --> 

<!--<br /><table border="0" cellpadding="0" cellspacing="0" width="150"><tr><td class="v14-header-2-small">New forum features</td></tr></table><table border="0" cellpadding="0" cellspacing="0" class="v14-gray-table-border"><tr><td width="150" class="no-padding"><table border="0" cellpadding="0" cellspacing="0" width="143"><tr valign="top"><td width="8"><img src="//www.ibm.com/i/c.gif" width="8" height="1" alt=""/></td><td><img src="//www.ibm.com/i/v14/icons/fw_bold.gif" height="16" width="16" border="0" vspace="3" alt=""/></td><td width="125"><p><a href="http://www.ibm.com/developerworks/community/forum/?S_TACT=105AGX01&amp;S_CMP=LEAF" class="smallplainlink">Try private messaging, 
read-tracking, and more
</a>
</p></td></tr></table></td></tr></table>--><!--END RESERVED FOR FUTURE USE INCLUDE FILES--><br><table border="0" cellpadding="0" cellspacing="0" width="150"><tbody><tr><td class="v14-header-2-small">Rate this page</td></tr></tbody></table><table class="v14-gray-table-border" border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="no-padding" width="150"><table border="0" cellpadding="0" cellspacing="0" width="143"><tbody><tr valign="top"><td width="8"><img alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" height="1" width="8"></td><td><img alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/d_bold.gif" border="0" height="16" vspace="3" width="16"></td><td width="125"><p><a href="#rate" class="smallplainlink"><b>Help us improve this content</b></a></p></td></tr></tbody></table></td></tr></tbody></table><br></td></tr></tbody></table><p>Level: Introductory</p><p><a href="#author">John Zukowski</a> (<a href="mailto:jaz@zukowski.net?subject=Swing%27s%20new%20JFormattedTextField%20component&amp;cc=jaloi@us.ibm.com">jaz@zukowski.net</a>), President, JZ Ventures, Inc.<br></p><p> 01 Jun  2002</p><blockquote>Accepting formatted input doesn't have to be difficult with input verifiers and focus listeners. In this installment of <i>Magic with Merlin</i>, John shows you how to use the new <code>JFormattedTextField</code> component to prompt for numbers, dates, and formatted input.</blockquote><!--START RESERVED FOR FUTURE USE INCLUDE FILES--><script language="JavaScript" type="text/javascript">
<!--
if (document.referrer&&document.referrer!="") { 
   // document.write(document.referrer);
   var q = document.referrer;
   var engine = q;
   var isG = engine.search(/google\.com/i);
   var searchTerms;
   //var searchTermsForDisplay;
   if (isG != -1) { 
	   var i = q.search(/q=/);
	   var q2 = q.substring(i+2);
	   var j = q2.search(/&/);
	   j = (j == -1)?q2.length:j;
	   searchTerms = q.substring(i+2,i+2+j);
	   if (searchTerms.length != 0) {
	       searchQuery(searchTerms);
	       document.write("<div id=\"contents\"></div>");
	   }
   } 
}
//-->
</script><!--END RESERVED FOR FUTURE USE INCLUDE FILES--><p>
The Java 2 Standard Edition (J2SE), version 1.4, adds two new Swing components to the palette of available GUI elements: <code>JSpinner</code> and <code>JFormattedTextField</code>. We covered the <code>JSpinner</code> component in the very first <a href="http://www.ibm.com/developerworks/library/j-mer0703/">Magic with Merlin</a> column; we'll now explore <code>JFormattedTextField</code>.
</p><p>
While the <code>JFormattedTextField</code> component looks like <code>JTextField</code>,
it acts completely different. In the simplest case, you can provide an
input mask like "(###) ###-####" for a telephone number, and it will
not accept any input that doesn't follow that format. In more
complicated cases, there is both a display formatter and an input
formatter. For example, the default date formatter permits scrolling
through the available months or days based upon where the cursor is
located while editing.
</p><p>
When working with <code>JFormattedTextField</code>,
acceptable input is either explicitly specified by the mask or
specified by a value for the component. In the latter case, the
component uses the Factory design pattern to look up the default
formatters for the class of the specified value. The <code>DefaultFormatterFactory</code> component comes with preinstalled formatters for dates, numbers, <code>java.text.Format</code> subclasses, and a catchall formatter for everything else.
</p><p>
Let's look at how to work with the component.
</p><p><a name="1"><span class="atitle">Configuring acceptable input</span></a></p><p>

Masked input is typically configured by using an instance of the <code>MaskFormatter</code> class. Found in the <code>javax.swing.text</code> package, <code>MaskFormatter</code>
works by using a series of characters to designate acceptable input.
Each of the eight characters in the series represents one character in
the input, as listed below:
</p><table border="1" cellpadding="3" cellspacing="0" width="600"><tbody><tr valign="top"><td>#</td><td>A number</td></tr><tr valign="top"><td>?</td><td>A letter</td></tr><tr valign="top"><td>A</td><td>A letter or number</td></tr><tr valign="top"><td>*</td><td>Anything</td></tr><tr valign="top"><td>U</td><td>A letter, with lowercase characters mapped to their uppercase equivalents</td></tr><tr valign="top"><td>L</td><td>A letter, with uppercase characters mapped to their lowercase equivalents</td></tr><tr valign="top"><td>H</td><td>A hexadecimal digit (A-F, a-f, 0-9)</td></tr><tr valign="top"><td>'</td><td>Used to escape another mask character</td></tr></tbody></table><p>
In addition to <code>MaskFormatter</code>, you can use the <code>DateFormat</code> and <code>NumberFormat</code> classes from the <code>java.text</code> package to specify the input format. Listing 1 shows some possible formats.
</p><table border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td class="code-outline"><pre class="displaycode">// Four-digit year, followed by month name and day of month,
// each separated by two dashes (--)
DateFormat format = 
  new SimpleDateFormat("yyyy--MMMM--dd");
DateFormatter df = new DateFormatter(format);
// US Social Security number
MaskFormatter mf1 = 
  new MaskFormatter("###-##-####");
// US telephone number
MaskFormatter mf2 = 
  new MaskFormatter("(###) ###-####");
</pre></td></tr></tbody></table><br><p>
Once you've specified the input format, you would then pass the formatter into the <code>JFormattedTextField</code> constructor, as shown below:
</p><table border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td class="code-outline"><pre class="displaycode">JFormattedTextField ftf1 = new 
      JFormattedTextField(df);
</pre></td></tr></tbody></table><br><p>
Depending on the formatter used, there are other configurable options. For instance, with <code>MaskFormatter</code>, you can set the placeholder character with <code>setPlaceholderCharacter(char)</code>. Also, for date fields, it helps if you initialize the field to some value so a user will know what input format is acceptable.
</p><br><table border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td><img src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/blue_rule.gif" alt="" height="1" width="100%"></td></tr></tbody></table><table class="no-print" align="right" cellpadding="0" cellspacing="0"><tbody><tr align="right"><td><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td valign="middle"><img src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/u_bold.gif" alt="" border="0" height="16" width="16"><br></td><td align="right" valign="top"><a href="#main" class="fbox"><b>Back to top</b></a></td></tr></tbody></table></td></tr></tbody></table><br><br><p><a name="1"><span class="atitle">Putting it all together</span></a></p><p>That's
really all there is to creating masked input fields. Listing 2 provides
a complete example for you to try out the new capabilities by combining
the previous code snippets. Figure 1 shows the display. Feel free to
adjust the individual masks to try out the other mask characters. </p><br><a name="figure1"><b>Figure 1. JFormattedTextField in action</b></a><br><img alt="JFormattedTextField in action" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/formatted.jpg" height="100" width="300"><br><table border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td class="code-outline"><pre class="displaycode">import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
import java.util.*;
import java.text.*;

public class FormattedSample {
  public static void main (String args[]) throws ParseException {
    JFrame f = new JFrame("JFormattedTextField Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = f.getContentPane();
    content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
    // Four-digit year, followed by month name and day of month,
    // each separated by two dashes (--)
    DateFormat format = 
      new SimpleDateFormat("yyyy--MMMM--dd");
    DateFormatter df = new DateFormatter(format);
    JFormattedTextField ftf1 = new 
      JFormattedTextField(df);
    ftf1.setValue(new Date());
    content.add(ftf1);
    // US Social Security number
    MaskFormatter mf1 = 
      new MaskFormatter("###-##-####");
    mf1.setPlaceholderCharacter('_');
    JFormattedTextField ftf2 = new 
      JFormattedTextField(mf1);
    content.add(ftf2);
    // US telephone number
    MaskFormatter mf2 = 
      new MaskFormatter("(###) ###-####");
    JFormattedTextField ftf3 = new 
      JFormattedTextField(mf2);
    content.add(ftf3);
    f.setSize(300, 100);
    f.show();
  }
}
</pre></td></tr></tbody></table><br><br><br><p><a name="resources"><span class="atitle">Resources</span></a></p><ul><li><a href="http://www-106.ibm.com/developerworks/forums/dw_forum.jsp?forum=182&amp;cat=10">Participate in the discussion forum</a>.<br><br></li><li>If you missed the explanation of the <code>JSpinner</code> component, check out <a href="http://www.ibm.com/developerworks/java/library/j-mer0703/">"Magic with Merlin: Swing's new Spinner component"</a> (<i>developerWorks</i>, July 2001).<br>
<br>
<br><br></li><li>Learn more about <code>JFormattedTextField</code>, including how to create your own input filters, from its <a href="http://java.sun.com/j2se/1.4.2/docs/guide/swing/1.4/ftf.html">formal documentation</a>.<br>
<br>
<br><br></li><li>Learn about the <code>JFormattedTextField</code> class with its <a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFormattedTextField.html">javadoc</a>.<br>
<br>
<br><br></li><li>Learn about the <code>MaskFormatter</code> class with its <a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/text/MaskFormatter.html">javadoc</a>.<br>
<br>
<br><br></li><li>If you aren't familiar with design patterns, you can start learning with David Gallardo's "<a href="http://www.ibm.com/developerworks/edu/j-dw-javapatt-i.html">Java design patterns 101</a>" tutorial, exclusively from <i>developerWorks</i> (January 2002).<br>
<br>
<br><br></li><li>Read the complete collection of <a href="http://www.ibm.com/developerworks/java/library/j-mercol.html">
<i>Magic with Merlin</i>
</a> articles by John Zukowski.<br>
<br>
<br><br></li><li>Find other Java technology resources in the <i>developerWorks</i>
<a href="http://www.ibm.com/developerworks/java/">Java technology zone</a>.<br></li></ul><br><br><p><a name="author"><span class="atitle">About the author</span></a></p><table border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td colspan="3"><img alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" height="5" width="100%"></td></tr><tr align="left" valign="top"><td><p><img alt="Author photo" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/p-zukowski.jpg" align="left" height="80" width="64"></p></td><td><img alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" height="5" width="4"></td><td width="100%"><p>
John Zukowski conducts strategic Java consulting with <a href="http://www.jzventures.com/">JZ Ventures, Inc.</a> and serves as the resident guru for a number of <a href="http://www.jguru.com/">jGuru</a>'s
community-driven <a href="http://www.jguru.com/faq/">Java FAQs</a>. His latest
books are <a href="http://www.amazon.com/exec/obidos/ASIN/078214022X/johnzukowshomefo/104-1464220-8871162">Mastering 
Java 2, J2SE 1.4</a> and <a href="http://www.amazon.com/gp/product/1893115984/104-1464220-8871162?n=283155">Learn 
Java with JBuilder 6</a>. Reach John at <a href="mailto:jaz@zukowski.net?Subject=Magic%20with%20Merlin&amp;cc=jaloi@us.ibm.com">jaz@zukowski.net</a>.
</p></td></tr></tbody></table><br><br><br><p class="no-print"><span class="atitle"><a name="rate">Rate this page</a></span></p><span class="no-print"><form action="http://www-128.ibm.com/developerworks/utils/RatingsHandler" method="post"><input value="1" name="SITE_ID" type="hidden"><input value="Magic with Merlin: Swing's new JFormattedTextField component" name="ArticleTitle" type="hidden"><input value="Java technology" name="Zone" type="hidden"><input value="http://www.ibm.com/developerworks/thankyou/feedback-thankyou.html" name="RedirectURL" type="hidden"><input value="10675" name="ArticleID" type="hidden"><input value="06012002" name="publish-date" type="hidden"><input name="author1-email" value="jaz@zukowski.net" type="hidden"><input name="author1-email-cc" value="jaloi@us.ibm.com" type="hidden"><script language="javascript" type="text/javascript">document.write('<input type="hidden" name="url" value="'+location.href+'" />');</script><img src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/gray_rule.gif" alt="" height="1" width="100%"><br><table class="v14-gray-table-border" border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td width="100%"><table border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td colspan="3"><p>Please take a moment to complete this form to help us better serve you.</p></td></tr><tr valign="top"><td width="140"><img src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" alt="" height="1" width="140"><br><p><label for="Goal">Did the information help you to achieve your goal?</label></p></td><td width="303"><img src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" alt="" height="6" width="303"><br><table border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td><input value="Yes" id="Goal" name="Goal" type="radio">Yes</td><td><input value="No" id="Goal" name="Goal" type="radio">No</td><td><input value="Don't know" id="Goal" name="Goal" type="radio">Don't know</td></tr></tbody></table></td><td width="100%">&nbsp;</td></tr><tr><td colspan="3"><img alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" height="12" width="8"></td></tr><tr valign="top"><td width="140"><img src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" alt="" height="1" width="140"><br><p><label for="Comments">Please provide us with comments to help improve this page:</label></p></td><td width="303"><img src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" alt="" height="6" width="303"><br><table border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td><textarea class="iform" cols="35" rows="5" wrap="virtual" id="Comments" name="Comments">&nbsp;</textarea></td></tr></tbody></table></td><td width="100%">&nbsp;</td></tr><tr><td colspan="3"><img alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" height="12" width="8"></td></tr><tr valign="top"><td width="140"><img src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" alt="" height="1" width="140"><br><p><label for="Rating">How useful is the information?</label></p></td><td width="303"><img src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" alt="" height="6" width="303"><br><table border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td align="left" width="60"><input value="1" id="Rating" name="Rating" type="radio">1</td><td align="left" width="60"><input value="2" id="Rating" name="Rating" type="radio">2</td><td align="left" width="60"><input value="3" id="Rating" name="Rating" type="radio">3</td><td align="left" width="60"><input value="4" id="Rating" name="Rating" type="radio">4</td><td align="left" width="63"><input value="5" id="Rating" name="Rating" type="radio">5</td></tr><tr><td align="left" width="60"><span class="greytext">Not</span><br><span class="greytext">useful</span></td><td align="left" width="60"><img src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" alt="" height="1" width="1"></td><td align="left" width="60"><img src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" alt="" height="1" width="1"></td><td align="left" width="60"><img src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" alt="" height="1" width="1"></td><td align="left" width="63"><span class="greytext">Extremely<br>useful</span></td></tr></tbody></table></td><td width="100%">&nbsp;</td></tr></tbody></table></td></tr></tbody></table><table class="v14-gray-table-border" border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td colspan="3"><img alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" height="8" width="8"></td></tr><tr><td width="8"><img alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" height="1" width="8"></td><td colspan="3"><input alt="Submit" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/submit.gif" border="0" height="21" type="image" width="120"></td></tr><tr><td colspan="3"><img alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" height="8" width="8"></td></tr></tbody></table></form><br></span><span class="no-print"><table align="right" cellpadding="0" cellspacing="0"><tbody><tr align="right"><td><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td valign="middle"><img src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/u_bold.gif" alt="" border="0" height="16" width="16"><br></td><td align="right" valign="top"><a href="#main" class="fbox"><b>Back to top</b></a></td></tr></tbody></table></td></tr></tbody></table><br><br></span></td><td width="10"><img alt="" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/c.gif" height="1" width="10"></td></tr></tbody></table></td></tr></tbody></table><!--FOOTER_BEGIN--><br>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody><tr>
<td class="bbg" height="19">
<table border="0" cellpadding="0" cellspacing="0">
<tbody><tr>
<td><span class="spacer">&nbsp;&nbsp;&nbsp;&nbsp;</span><a class="mainlink" href="http://www.ibm.com/ibm/">About IBM</a></td>
<td class="footer-divider" width="27">&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td><a class="mainlink" href="http://www.ibm.com/privacy/">Privacy</a></td>
<td class="footer-divider" width="27">&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td><a class="mainlink" href="http://www.ibm.com/contact/">Contact</a></td>
</tr>
</tbody></table>
</td>
</tr>
</tbody></table>

<script type="text/javascript" language="JavaScript1.2" src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/stats.js"></script>
<noscript><img src="Magic%20with%20Merlin%20%20Swing%27s%20new%20JFormattedTextField%20component_files/uc.GIF" alt="" border="0" height="1" width="1"></noscript><!--FOOTER_END--><!--XSLT stylesheet used to transform this file:  dw-document-html-5.8.xsl--></body></html>